<template>
	<view class="commodity">
		<view class="add">
			<view class="item">
				{{merchantText}}
			</view>
			<!-- <view class="item">
				可获得10万+车主到店消费权益
			</view>
			<view class="item">
				全平台优先曝光
			</view>
			<view class="item">
				成为团洗会员权益商户
			</view> -->
		</view>
		<view class="list f-column" v-for="(item,index) in list" :key="index">
			<view class="list_item" :style="{background:item.status==1?'#fff':'#fff'}">
				<view class="content">
					<view class="item" @click="selectItem(item)">
						<view class="icon">
							<!-- <image :src="item.icon" mode="" class="image"></image> -->
							<imgUpload code="zsbm" placeholder="请上传图片" :limit="1" @upImgs="upImgs" :imageArr="item.imgs"
								:disable="type==1" :showdel="true"></imgUpload>
						</view>
						<view class="entityList">
							<view class="topName" style="font-size: 30rpx;">
								<text class="text">{{item.name}}</text>
								<view class="right" style="color: #2CB9A4;">
									平均上限价￥{{item.price}}
								</view>
							</view>
							<view class="name">
								<text class="text"><!-- {{item.switchStatus}} -->是否含有此类服务</text>
								<view class="right">
									<u-switch v-model="item.switchStatus" activeColor="#2CB9A4" size="18"
										@change="changeSwitch($event,item,index)"></u-switch>
								</view>
							</view>
							<view class="name">
								<text class="text">会员价格设置</text>
								<view class="right" style="width: 35%;">
									<u--input placeholder="请输入价格" border="surround"
										v-model="item.switchStatus?item.goods.price:item.firstPrice"
										:disabled="item.goods_status&&item.goods.status==1?false:true"
										@blur="changeValue($event,item,index)"></u--input>
								</view>
							</view>
						</view>
					</view>
					<view class="commodiyBot">
						{{merchantTip}}
					</view>
				</view>
			</view>
		</view>
		<view class="noMore" v-if="last_page==form.page&&list.length!=0"
			style="text-align: center;color: #797575;margin-top: 20rpx;padding-bottom: 60rpx;">
			数据加载完成...
		</view>
		<u-empty text="暂无列表哦~~" mode="list" v-if="list.length==0" margin-top='50'></u-empty>
		<u-modal :show="showDelete" :closeOnClickOverlay="true" :showCancelButton='true' :title="'提示'"
			@confirm='updateGoods1' @cancel='showDelete=false' :content="merchantTip1"
			confirmColor="#2CB9A4"></u-modal>
	</view>
</template>

<script>
	import imgUpload from '@/components/common/imgUpload.vue'
	import {
		openSwitch,
		openTime,
		merchantConfig
	} from '@/request/api.js'
	import {
		getMerchantCard,
		updateGoods
	} from '@/request/api.js'
	export default {
		components: {
			imgUpload
		},
		data() {
			return {
				showDelete: false,
				altAmount:'0',
				value1: true,
				list: [],
				form: {
					page: 1
				},
				last_page: 1,
				updateData: {

				},
				formData: {
					images: '',
					card_type: 1,
					price: '',
					card_type_id: '',
					id: ''
				},
				selectData: null,
				merchantTip: '',
				merchantText: '',
				tempItem :null,
				merchantTip1:'会员价格大于团洗价格将不加入团洗！',
			};
		},
		onReachBottom() {
			// console.log(111)
			if (this.form.page < this.last_page) {
				this.form.page = this.form.page + 1
				this.getList()
			}
		},
		onLoad() {
			this.form.page = 1
			this.list = []
			this.getList()
			this.getMerchantConfig()
		},
		methods: {
			async getMerchantConfig() {
				let res = await merchantConfig()
				this.merchantText = res.data.merchant_text
				this.merchantTip = res.data.merchant_tip
				this.merchantTip1 = res.data.merchant_tallTip
				// console.log(res)
			},
			getSwitch(item) {
				//如果item.status为false直接返回return false;
				if (item.goods_status == false) {
					return false;
				} else {
					if (item.goods.status == 1) { //等于1返回true
						return true;
					} else {
						return false;
					}
				}
			},
			changeSwitch(value, item, index) {
				console.log(value, item, 222)
				let data = {
					card_type: 1,
					id: item.goods_status ? item.goods.id : '',
					status: value ? 1 : 0,
					card_type_id: item.id,
				}
				// console.log(this.list, 'list')
				this.$forceUpdate()
				// console.log(this.list[index].switchStatus, 'switchStatus2')
				updateGoods(data).then(res => {
					if (res.code == 200) {
						uni.$u.toast('修改会员状态成功')
						this.list[index].switchStatus = value
						this.list[index].goods_status = true
						if (!this.list[index].goods) {
							this.form.page = 1
							this.list = []
							this.getList()
							return;
						}
						this.list[index].goods.status = value ? 1 : 0

					}
				})
			},
			changeValue(value, item, index) {
				var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
				// let reg = / (([1-9]{1}\d*)(.\d{1,2})?)|(0{1}.\d{1,2})/
				console.log(value)
				if (!reg.test(value)) {
					uni.$u.toast('价格为只能输入数字和小数点两位')
					return false;
				}
				let data = {
					card_type: 1,
					price: value,
					id: item.goods_status ? item.goods.id : '',
					card_type_id: item.id,
				}
				this.updateData = data
				this.altAmount = value
				this.tempItem = {//记录下要修改的下标
					index:index
				}
				if (Number(value) > Number(item.price)) {
					this.showDelete = true;
					return false;
				}
				this.updateGoods().then(price=>{
					console.log(price,555)
					this.list[index].switchStatus?this.list[index].goods.price = price:this.list[index].firstPrice = price
				})
				

			},
			updateGoods1(){
				this.updateGoods().then(price=>{
					console.log(price,555)
					this.list[this.tempItem.index].switchStatus?this.list[this.tempItem.index].goods.price = price:this.list[this.tempItem.index].firstPrice = price
				})
			},
			updateGoods() {
				 return new Promise((resolve, reject) => {
					updateGoods(this.updateData).then(res => {
						if (res.code == 200) {
							setTimeout(() => {
								uni.$u.toast('修改会员价格成功')
							}, 500)
							this.showDelete = false;
							resolve(this.overFormat(this.altAmount)) 
							// this.form.page = 1
							// this.list = []
							// this.getList()
						}
					})
				});
			},
			overFormat(th) {
				var v = th;
				if (v === '') {
					v = '0.00';
				} else if (v === '0') {
					v = '0.00';
				} else if (v === '0.') {
					v = '0.00';
				} else if (/^0+\d+\.?\d*.*$/.test(v)) {
					v = v.replace(/^0+(\d+\.?\d*).*$/, '$1');
					v = inp.getRightPriceFormat(v).val;
				} else if (/^0\.\d$/.test(v)) {
					v = v + '0';
				} else if (!/^\d+\.\d{2}$/.test(v)) {
					if (/^\d+\.\d{2}.+/.test(v)) {
						v = v.replace(/^(\d+\.\d{2}).*$/, '$1');
					} else if (/^\d+$/.test(v)) {
						v = v + '.00';
					} else if (/^\d+\.$/.test(v)) {
						v = v + '00';
					} else if (/^\d+\.\d$/.test(v)) {
						v = v + '0';
					} else if (/^[^\d]+\d+\.?\d*$/.test(v)) {
						v = v.replace(/^[^\d]+(\d+\.?\d*)$/, '$1');
					} else if (/\d+/.test(v)) {
						v = v.replace(/^[^\d]*(\d+\.?\d*).*$/, '$1');
						ty = false;
					} else if (/^0+\d+\.?\d*$/.test(v)) {
						v = v.replace(/^0+(\d+\.?\d*)$/, '$1');
						ty = false;
					} else {
						v = '0.00';
					}
				}
				console.log(v,'金额')
				return v
			},
			selectItem(e) {
				this.selectData = e
			},
			upImgs(e) {
				let arr = e.images
				this.formData.images = arr.join(',') //
				if (arr.length == 0) return false;
				let data = {
					card_type: 1,
					id: this.selectData.goods_status ? this.selectData.goods.id : '',
					card_type_id: this.selectData.id,
					images: arr.join(',')
				}
				updateGoods(data).then(res => {
					if (res.code == 200) {
						uni.navigateTo({
							url: '/my_pages/components/success?title=添加商品图片成功'
						})
					}
				})
			},
			async getList() {
				let res = await getMerchantCard(this.form)
				this.list = this.list.concat(res.data.data)
				this.list.forEach(item => {
					item['switchStatus'] = this.getSwitch(item) //转换switch的状态
					item['firstPrice'] = item.price
					item['imgs'] = []
					if (!item.goods) {
						return;
					}
					if (item.goods.images.length != 0) {
						item.goods.images.map(el => {
							item['imgs'].push({
								url: el
							})
						})
					}
				})
				this.last_page = res.data.last_page
				console.log(this.list, '商品列表')
			},
			addCommodity() {
				uni.navigateTo({
					url: '/my_pages/addCommInfo/addCommInfo'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	::v-deep .u-input {
		padding-top: 2px !important;
		padding-bottom: 2px !important;
	}

	.commodity {
		font-size: 28rpx;
		padding: 20rpx;

		.add {
			color: #2CB9A4;
			background: #C0EAE4;
			box-shadow: 0px 0px 10px rgba(8, 23, 56, 0.05);
			border-radius: 8px;
			padding: 20rpx;

			.item {}
		}

		.list {
			margin-top: 20rpx;

			.content {
				background-color: #fff;
				// box-shadow: 0px 0px 10px rgba(8, 23, 56, 0.05);
				border-radius: 10px;
				// padding: 20rpx;
				margin-top: 20rpx;

				.conBot {
					align-items: flex-end;

					.f-row {
						display: flex;
						align-items: flex-end;
					}

					.goNear {
						background: #2CB9A4;
						color: #fff;
						height: 60rpx;
						justify-content: center;
						align-items: center;
						border-radius: 10rpx;
						padding: 10rpx 20rpx;
						font-size: 32rpx;
						align-items: center;
					}

					display: flex;
					justify-content: space-between;
					color: #2CB9A4;

					.down {
						background: #C0EAE4;
						justify-content: center;
						display: flex;
						flex-direction: column;
						align-items: center;
						padding: 10rpx;
						font-size: 22rpx;
						border-radius: 10rpx;
						margin-right: 10rpx;
					}

					.down_price {
						margin-right: 10rpx;
					}

					.price {
						margin-right: 10rpx;
					}
				}
			}

			.list_item {
				display: flex;
				flex-direction: column;
				background: #2CB9A4;
				border-radius: 20rpx;
				padding: 20rpx;

				.title {
					color: #fff;
					font-size: 30rpx;
					font-weight: 600;
				}

				.item {
					display: flex;
					color: #505050;
					margin: 15rpx 0;
					border-bottom: 0.5px solid rgba(217, 217, 217, 0.3);
					padding-bottom: 15rpx;
					box-sizing: border-box;
					background: #fff;
					border-radius: 20rpx;

					.icon {
						// width: 100rpx;
						// height: 100rpx;

						// .image {
						// 	width: 100%;
						// 	height: 100%;
						// }
					}

					.entityList {
						flex: 1;
						display: flex;
						flex-direction: column;
						overflow: hidden;
						text-overflow: ellipsis;
						justify-content: space-between;

						.name {
							display: flex;
							justify-content: space-between;
							margin-left: 15rpx;
							white-space: nowrap;
							overflow: hidden;
							text-overflow: ellipsis;
							color: #BABABA;
							font-size: 26rpx;
							margin-top: 10rpx;

							.text {
								width: 60%;
								overflow: hidden;
								text-overflow: ellipsis;
								white-space: nowrap;
							}

							.image {
								width: 26rpx;
								height: 26rpx;
								margin-right: 10rpx;
							}
						}

						.topName {
							display: flex;
							justify-content: space-between;
							margin-left: 15rpx;
							// white-space: nowrap;
							overflow: hidden;
							text-overflow: ellipsis;
							color: #000;
							font-weight: 600;
							font-size: 26rpx;

							.text {
								width: 45%;
								overflow: hidden;
								text-overflow: ellipsis;
								white-space: wrap;
							}

							.image {
								width: 26rpx;
								height: 26rpx;
								margin-right: 10rpx;
							}
						}
					}
				}

				.commodiyBot {
					display: flex;
					flex-direction: row-reverse;
					color: #BABABA;
					font-size: 28rpx;
					.botBtn {
						margin-left: 30rpx;
					}
				}
			}
		}
	}
</style>